import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { FormItem } from '../../../../components/form/formItem'
import { orderBaseStyle } from '../../create/_common/style';
import OrderCreateMethods from '../../OrderCreateMethods';
export class AddressCpt extends Component {
	constructor(props) {
		super(props);
		let { value } = this.props.data || {};
		this.state = {
			value
		}
	}

	componentWillReceiveProps(props) {
		let { value } = props.data || {};
		this.setState({
			value
		})
	}

	// 点击跳转选地址
	_addressClick = (params1) => {
		let { tCode, orderType, companyId, deliveryType, agencyCompanyId, agencyCompanyName, companyName, companyIdBuy, companyNameBuy } = this.props.data.paramsData || {};
		let { navigation } = this.props.data || {};
		// 如果是背靠背
		if (tCode === 'C') {
			let lsCompanyId = params1 == 'destAddressId' ? companyId : companyIdBuy;
			let lsCompanyName = params1 == 'destAddressId' ? companyName : companyNameBuy;
			// 供应商配送 和 化销目的地  
			if (deliveryType == 'P') {
				lsCompanyId = companyId;
				lsCompanyName = companyName;
			} else if (params1 == 'destAddressId' && deliveryType == 'S') {
				lsCompanyId = companyId;
				lsCompanyName = companyName;
			}
			navigation.navigate('DeliveryAddress', {
				type: 'order',
				companyId: lsCompanyId,
				companyName: lsCompanyName,
				oppoDeliveryType: deliveryType,
				orderType: orderType,
				addressType: params1,  // destAddressId  ->  目的地   srcAddressId ->  出库地
				tCode: tCode,
				getAddressCallBack: (data) => {
					this._addressCallBack(data, params1)
				}
			});

		} else {
			companyId = companyId;
			// 代理的发货地址，id取泉州石化
			if (tCode == 'A' && params1 == "srcAddressId") {
				companyId = agencyCompanyId;
				companyName = agencyCompanyName;
			}
			navigation.navigate('DeliveryAddress', {
				type: 'order',
				companyId,
				companyName,
				deliveryType: deliveryType,
				orderType: orderType,
				addressType: params1,  // destAddressId  ->  目的地   srcAddressId ->  出库地
				tCode: tCode,
				isHiddenSwitch: OrderCreateMethods._switchEnterprise({ tCode, orderType, deliveryType }),
				getAddressCallBack: (data) => {
					this._addressCallBack(data, params1)
				}
			});
		}
	}


	// 回填地址
	_addressCallBack = (data, id) => {
		let { addressCallback, index } = this.props.data || {};
		let { value } = this.state;
		if (!data.length) return;
		value = (data[0].addressName || '') + ` ${(data[0].fullName || '') + (data[0].detailAddress || '')}`;
		//value = data[0].addressName ? data[0].addressName : data[0].detailAddress;
		this.setState({
			value
		}, () => {
			addressCallback && addressCallback({ id, addrId: data[0].addrId, value, index })
		})
	}


	render() {
		let { title, id, info, editable } = this.props.data || {};
		let { value } = this.state;
		return (
			<View style={editable ? orderBaseStyle.disableStyle : null}>
				<FormItem required={true} info={info || false} type={id ? 'select' : 'input'} label={title || '标题'} editable={!!id} handleCallBack={() => { if (editable) return; this._addressClick && this._addressClick(id, title) }} />
				<Text onPress={() => { if (editable) return; id && this._addressClick && this._addressClick(id, title) }} style={orderBaseStyle.address}>{value || '请选择地址...'}</Text>
			</View >)
	}
}